package com.example.redis.topic;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;

@Slf4j
public class RedisPubSub {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    private ChannelTopic topic = new ChannelTopic("/redis/pubsub");

    /**
     * 推送消息
     *
     * @param publisher
     * @param content
     */
    public void publish(String publisher, String content) {
        log.info("message send {} by {}", content, publisher);

        redisTemplate.convertAndSend(topic.getTopic(), content);
    }
}
